import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import java.util.LinkedList;
import java.util.ListIterator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JFrameInscribirEstudiante extends javax.swing.JDialog {
	private JPanel jPanelInscribirEstudiante;
	private JLabel jLblApellido;
	private JLabel jLblNombre;
	private JTextField jTxtApellido;
	private JButton jButtonSalir;
	private JButton jButtonGuardar;
	private JTextField jTxtNombre;
	private JTextField jTxtCedula;
	private JLabel jLabel1;
	
	private Estudiante estu;
	private int cedula;
	private Seccion secc;
	private int opc1;
	LinkedList<Estudiante> LisEstu1 = new LinkedList<Estudiante>();
	private int numero;
	JFrameSeccion jfc;
	private JLabel jLabel2;
	JFrameAbrirSeccion jfas;
	
	
	
	public JFrameInscribirEstudiante(JFrame frame, Seccion seccion) {
		super(frame);
		initGUI();
		secc=seccion;
	}
	
	private void initGUI() {
		try {
			setModal(true);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			numero = 0;
			estu = null;
			cedula=0;
			opc1 = 0;
			
			this.setTitle("Inscribir Estudiante");
			{
				jPanelInscribirEstudiante = new JPanel();
				getContentPane().add(jPanelInscribirEstudiante);
				//jPanelInscribirEstudiante.setLayout(jPanelInscribirEstudianteLayout);
				jPanelInscribirEstudiante.setBounds(24, 32, 597, 307);
				jPanelInscribirEstudiante.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(0,0,0)));
				jPanelInscribirEstudiante.setLayout(null);
				{
					jLabel1 = new JLabel();
					jPanelInscribirEstudiante.add(jLabel1);
					jLabel1.setText("Cedula Estudiante");
					jLabel1.setBounds(26, 45, 176, 24);
					jLabel1.setFont(new java.awt.Font("Comic Sans MS",3,14));
				}
				{
					jLblApellido = new JLabel();
					jPanelInscribirEstudiante.add(jLblApellido);
					jLblApellido.setText("ApellidoEstudiante");
					jLblApellido.setBounds(24, 144, 171, 26);
					jLblApellido.setFont(new java.awt.Font("Comic Sans MS",3,14));
				}
				{
					jLblNombre = new JLabel();
					jPanelInscribirEstudiante.add(jLblNombre);
					jLblNombre.setText("Nombre Estudiante");
					jLblNombre.setBounds(24, 95, 178, 24);
					jLblNombre.setFont(new java.awt.Font("Comic Sans MS",3,14));
				}
				{
					jTxtCedula = new JTextField();
					jPanelInscribirEstudiante.add(jTxtCedula);
					jTxtCedula.setBounds(220, 48, 229, 22);
					jTxtCedula.setFont(new java.awt.Font("Comic Sans MS",3,14));
				}
				{
					jTxtNombre = new JTextField();
					jPanelInscribirEstudiante.add(jTxtNombre);
					jTxtNombre.setBounds(220, 98, 229, 22);
					jTxtNombre.setFont(new java.awt.Font("Comic Sans MS",3,14));
				}
				{
					jTxtApellido = new JTextField();
					jPanelInscribirEstudiante.add(jTxtApellido);
					jTxtApellido.setBounds(220, 148, 229, 22);
					jTxtApellido.setFont(new java.awt.Font("Comic Sans MS",3,14));
				}
				{
					jButtonGuardar = new JButton();
					jPanelInscribirEstudiante.add(jButtonGuardar);
					jButtonGuardar.setText("Guardar Estudiante");
					jButtonGuardar.setBounds(24, 248, 168, 22);
					jButtonGuardar.setFont(new java.awt.Font("Comic Sans MS",3,14));
					jButtonGuardar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonGuardarActionPerformed(evt);								
						}
					});  
				}
				{
					jButtonSalir = new JButton();
					jPanelInscribirEstudiante.add(jButtonSalir);
					jButtonSalir.setText("Salir");
					jButtonSalir.setBounds(463, 248, 100, 22);
					jButtonSalir.setFont(new java.awt.Font("Comic Sans MS",3,14));
					jButtonSalir.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonSalirActionPerformed(evt);								
						}
					});  
				}
				{
					jLabel2 = new JLabel();
					jPanelInscribirEstudiante.add(jLabel2);
					jLabel2.setText("Registro de Estudiantes");
					jLabel2.setBounds(232, 13, 191, 15);
					jLabel2.setFont(new java.awt.Font("Comic Sans MS",3,14));
				}
			}
			pack();
			this.setSize(661, 396);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

	private void jButtonSalirActionPerformed(ActionEvent evt) {
		
		this.dispose();
		//JFrameRetirarEstudiante framer = new JFrameRetirarEstudiante();
		//framer.setVisible(true);
		
	}

	private void jButtonGuardarActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		do {
			opc1 = 1;
			if(jTxtCedula.getText().trim().equals("") || jTxtNombre.getText().trim().equals("") || jTxtApellido.getText().trim().equals("") ){
				JOptionPane.showMessageDialog(null, "Debe llenar todos los campos para Guardar", "Informacion", JOptionPane.INFORMATION_MESSAGE);
			} else {
				try {
					cedula = Integer.parseInt(jTxtCedula.getText().trim());
					ListIterator<Estudiante> it = LisEstu1.listIterator();
					while (it.hasNext()) 
					{
						Estudiante estu1 = it.next();
						if (estu1.getCedula() == cedula) {
							JOptionPane.showMessageDialog(null, "La cedula del estudiante ya existe", "Informacion", JOptionPane.INFORMATION_MESSAGE);
							jTxtCedula.setText("");
							jTxtNombre.setText("");
							jTxtApellido.setText("");
							
							return;
						}
					}
					
					estu = new Estudiante(Integer.parseInt(jTxtCedula.getText().trim()), jTxtNombre.getText().trim(), jTxtApellido.getText().trim());
					//secc = new Seccion(2 /* jfc.SacarNumero() */,pro);
					LisEstu1.add(estu);
					secc.inscribirEstudiante(estu);
					jTxtCedula.setText("");
					jTxtNombre.setText("");
					jTxtApellido.setText("");
					
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(null, "La cedula del profesor debe ser valores numericos", "Informacion", JOptionPane.INFORMATION_MESSAGE);
					opc1 = 0;
					jTxtCedula.setText("");
					jTxtNombre.setText("");
					jTxtApellido.setText("");
				}
			}
		} while (opc1 == 0);
		
	}

}
